/**
 * 主界面
 * 大厅界面
*/  
cc.Class({
    extends: cc.Component,

    properties: {
        labGoldNum: cc.Label, //已赚元宝数
        labpeopleNum: cc.Label,  //已注册人数
        lotteryLists: [cc.Node],//彩种列表  
        roomPopPre: cc.Prefab //房间列表预制
    },

    // use this for initialization
    onLoad: function () {
  
        //默认赋初值
        for(var i=0;i<4;++i){
            var code = Room.getLotteryCode(i);
        //    cc.log('hallui 初始化列表',code);
            if(code == 0)
                continue;
            this.loadOneLottery(i,code);
        }

        //请求获取初始化数据
        this.getHallList(function(data){
            this.initHallList(data);
            if(Utils.isEffective(data.ActivityData))
            {
           //     cc.log("managecenter 活动数"+ data.ActivityData.length);
                CL.MAINMANAGE.loadImg(data.ActivityData);
            }
            else{
                CL.MAINMANAGE.loadImg([]);
            }
               
        }.bind(this));
    },

    //加载一个彩种
    loadOneLottery:function(index,code){
        var indexStr = index+1;
        var childName = "hall_game_name_0"+ indexStr;
        this.lotteryLists[index].getComponent(cc.Sprite).spriteFrame = CL.MAINMANAGE.getLotteryIconById(code);
        this.lotteryLists[index].getChildByName(childName).getComponent(cc.Sprite).spriteFrame = CL.MAINMANAGE.getLotteryNameById(code);
        if(this.lotteryLists[index].getComponent(cc.Button).clickEvents.length == 0)
        {
            var checkEventHandler = new cc.Component.EventHandler();
            checkEventHandler.target = this.node; 
            checkEventHandler.component = "HallUI";
            checkEventHandler.handler = "onItemBtn";
            checkEventHandler.customEventData = code;
            this.lotteryLists[index].getComponent(cc.Button).clickEvents.push(checkEventHandler);
        }
        else
        {
       //     cc.log("hallui 改变彩种code:"+code);
            this.lotteryLists[index].getComponent(cc.Button).clickEvents[0].customEventData = code;
        }
            

        this.lotteryLists[index].active = true;
    },

    //初始化数据
    getHallList: function(callback){
        var self = this;
        var recv = function recv(ret) {
            ComponentsUtils.unblock();  
            if (ret.Code !== 0 && ret.Code != 100) {
                ComponentsUtils.showTips(ret.Msg);
            } else {

                //登录模式 1：游客登录 2：手机号登录 3：微信登录 4：QQ登录 5：其他
                if(!User.getIsvermify())
                {
                    if(ret.IsVerifyToken)//验证通过
                    {
                        User.setLoginToken(ret.Token);
                        User.upDateUserInfo(false,ret.LoginData);
                    }
                    else//游客登录
                    {
                        User.setLoginToken(ret.Token);
                        User.upDateUserInfo(true,"");
                    }
                    User.setIsvermify(ret.IsVerifyToken);
                }
 
                if(ret.Code != 100)
                {
                    CL.MAINMANAGE.onConnectSK();
                }

                if (callback != null) {
                    callback(ret);
                }

                User.setSmGame(ret.GameData);
            }
            
        };
    
        var data = {
            Equipment:Utils.getEquipment(),
            Token:User.getLoginToken()
        };
        
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.INIT, data, recv.bind(this),"POST");  
        ComponentsUtils.block();  
    },

    //显示大厅数据界面
    initHallList: function(data){
        cc.info('hallui 显示大厅数据',data);
        this.labGoldNum.string = data.SystemTotalWin + '宝';
        var count = 0;
        for(var i=0;i<data.LotteryData.length;++i){
            var code =data.LotteryData[i].LotteryCode;
            if(code =='40010' ||code =='40020' ||code =='301' ||code =='202'){
                Room.setLotteryCode(count,data.LotteryData[i].LotteryCode);
                this.loadOneLottery(count,data.LotteryData[i].LotteryCode);
                count++;
            }
        }  
    },

    //进入一个彩种房间
    onItemBtn:function(event,customEventData){ 
        //停不停售
        var canvas = cc.find("Canvas");
        var roomPopPre = cc.instantiate(this.roomPopPre);
        roomPopPre.getComponent('roomlist_pop').init(customEventData);
        canvas.addChild(roomPopPre);

    }
});
